package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.RoutinePathologySamplingPlatformMapper;
import com.zhijian.medical.entity.po.RoutinePathologySamplingPlatform;
import com.zhijian.medical.entity.vo.request.RoutinePathologySamplingPlatformSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.RoutinePathologySamplingPlatformSearchVo;
import com.zhijian.medical.entity.vo.response.RoutinePathologySamplingPlatformVo;
import com.zhijian.medical.service.RoutinePathologySamplingPlatformService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病例取材台业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-06-09 18:55:36
 */
@Slf4j
@Service
public class RoutinePathologySamplingPlatformServiceImpl implements RoutinePathologySamplingPlatformService {

    @Autowired
    private RoutinePathologySamplingPlatformMapper mapper;

    @Override
    public RoutinePathologySamplingPlatform selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutinePathologySamplingPlatformVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutinePathologySamplingPlatform routinePathologySamplingPlatform) {
        Date now = new Date();
        routinePathologySamplingPlatform.setCreateTime(now);
        routinePathologySamplingPlatform.setUpdateTime(now);
        return mapper.insert(routinePathologySamplingPlatform) > 0 ? routinePathologySamplingPlatform.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutinePathologySamplingPlatform routinePathologySamplingPlatform) {
        Date now = new Date();
        routinePathologySamplingPlatform.setCreateTime(now);
        routinePathologySamplingPlatform.setUpdateTime(now);
        return mapper.insertSelective(routinePathologySamplingPlatform) > 0 ? routinePathologySamplingPlatform.getId() : 0L;
    }

    @Override
    public boolean update(RoutinePathologySamplingPlatform routinePathologySamplingPlatform) {
        Date now = new Date();
        routinePathologySamplingPlatform.setUpdateTime(now);
        return mapper.updateByPrimaryKey(routinePathologySamplingPlatform) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutinePathologySamplingPlatform record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutinePathologySamplingPlatformSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologySamplingPlatform routinePathologySamplingPlatform = new RoutinePathologySamplingPlatform();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologySamplingPlatform);
        return this.insert(routinePathologySamplingPlatform) != 0;
    }

    @Override
    public boolean modify(RoutinePathologySamplingPlatformSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologySamplingPlatform old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutinePathologySamplingPlatform routinePathologySamplingPlatform = new RoutinePathologySamplingPlatform();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologySamplingPlatform);
        return this.update(routinePathologySamplingPlatform);
    }

    @Override
    public RoutinePathologySamplingPlatformVo selectOne(RoutinePathologySamplingPlatformSearchVo routinePathologySamplingPlatformSearchVo) {
        return mapper.selectOne(routinePathologySamplingPlatformSearchVo);
    }

    @Override
    public Integer count(RoutinePathologySamplingPlatformSearchVo routinePathologySamplingPlatformSearchVo) {
        return mapper.count(routinePathologySamplingPlatformSearchVo);
    }

    @Override
    public List<RoutinePathologySamplingPlatformVo> selectList(RoutinePathologySamplingPlatformSearchVo routinePathologySamplingPlatformSearchVo) {
        return mapper.selectList(routinePathologySamplingPlatformSearchVo);
    }

    @Override
    public Page<RoutinePathologySamplingPlatformVo> selectPage(RoutinePathologySamplingPlatformSearchVo routinePathologySamplingPlatformSearchVo) {
        AssertUtil.check(Objects.isNull(routinePathologySamplingPlatformSearchVo.getPageNum()) || Objects.isNull(routinePathologySamplingPlatformSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routinePathologySamplingPlatformSearchVo);
        //分页
        Page<RoutinePathologySamplingPlatformVo> page = new Page<>(true, count, routinePathologySamplingPlatformSearchVo.getPageNum(), routinePathologySamplingPlatformSearchVo.getPageSize());
        routinePathologySamplingPlatformSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutinePathologySamplingPlatformVo> responseVos = this.selectList(routinePathologySamplingPlatformSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutinePathologySamplingPlatform> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutinePathologySamplingPlatformSearchVo routinePathologySamplingPlatformSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routinePathologySamplingPlatformSearchVo);
    }

}